player     = {}
fish       = {}
cwide      = 520
chigh      = 333

love.window.setTitle(' Hello Game Wörld ')
love.window.setMode(cwide, chigh)
	
function love.load()
    fish.x    = 0
    fish.y    = 0
    fish.img  = love.graphics.newImage( 'images/fish.png' )
    fish.wide = fish.img:getWidth()
    fish.high = fish.img:getHeight()
    player.x     = 150
    player.y     = 150
    player.img   = love.graphics.newImage('images/tux.png')
    player.wide  = player.img:getWidth()
    player.high  = player.img:getHeight()
    player.speed = 10
end

function love.update(dt)
    if love.keyboard.isDown("right") then
        if player.x <= (love.graphics.getWidth() - player.img:getWidth()) then
	player.x = player.x+player.speed
	rotate_right()
    end

    elseif love.keyboard.isDown("left") then
        if player.x <= (love.graphics.getWidth() + player.wide - player.img:getWidth()) then
	player.x = player.x-player.speed
	rotate_left()
    end
    elseif love.keyboard.isDown("up")  then
	player.y = player.y-player.speed    

    elseif love.keyboard.isDown("down") then
	player.y = player.y+player.speed
end

    if CheckCollision(fish.x,fish.y,151,61, player.x,player.y,player.img:getWidth(),player.img:getHeight() ) then
        fdead()
    else
	falive()
    end

    automove(fish,1,0,fish.wide,fish.high)
end

function love.draw()
    love.graphics.draw(player.img,player.x,player.y,0,1,1,0, 0)
    love.graphics.draw(fish.img, fish.x, fish.y, 0, 1, 1, 0, 0)    
end

function automove(obj,x,y,ox,oy)
    if obj.x == cwide-fish.img:getWidth() then
        edgeright = 0
	elseif obj.x == 0 then
	edgeright = 1
    end

    if edgeright == 1 then
        obj.x = obj.x + x
    else
        obj.x = obj.x - x
    end
end

function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2)
    return x1 < x2+w2 and
        x2 < x1+w1 and
        y1 < y2+h2 and
        y2 < y1+h1
    end

function rotate_left()
    player.img = love.graphics.newImage('images/tuxleft.png')
end

function rotate_right()
    player.img = love.graphics.newImage('images/tux.png' )
end

function falive()
    fish.img = love.graphics.newImage('images/fish.png')
end

function fdead()
fish.img = love.graphics.newImage('images/fishbones.png')
end